
# 字符串、列表、元组、字典、集合都可以被for循环，说明他们都是可迭代的
from typing import Iterator

string = "asdfasdf"
list = [1,2,3,4]
tuple = (1,2,3,4)
dict = {1:2,3:4}
set = {1,2,3,4}

print(isinstance(string,Iterable))
print(isinstance(list,Iterable))
print(isinstance(tuple,Iterable))
print(isinstance(dict,Iterable))
print(isinstance(set,Iterable))
print(isinstance(range(100000000),Iterator))

list_data = [1,2,3,4]
list_iter = list_data.__iter__()
while True:
    try:
        item = list_iter.__next__()
        print(item)
    except StopIteration:
        break

# 迭代器遵循迭代器协议：必须拥有__iter__方法和__next__方法。
# 迭代器有的好处是可以节省内存。

# print('__next__' in dir(range(1)))#查看'__next__'是不是在range()方法执行之后内部是否有__next__